<?php
    class Transporteur {
        //Alias sur table : t
        public $id_transporteur;
        public $libelle;
        public $tel;
        public $email;
        public $franco_port;
        public $frais_port;
        public $tva;
        public $date_ajout;
        public $date_modif;
        public $valide;
		
        //Constructeur complet
        //(Tva)tva
        public function __construct($id_transporteur, $libelle, $tel, $email, $franco_port, $frais_port, $tva, $date_ajout, $date_modif, $valide) {
            $this->setId($id_transporteur);
            $this->setLibelle($libelle);
            $this->setEmail($email);
            $this->setFrancoPort($franco_port);
            $this->setFraisPort($frais_port);
            $this->setTva($tva);
            $this->setDateAjout($date_ajout);
            $this->setDateModif($date_modif);
            $this->setValide($valide);
        }
	
        //Retourne le libellé de ce Transporteur
        //Paramètre : aucun
        //Retourne (String)
        public function __toString() {
            return $this->libelle;
        }
        
        //Ajoute un objet Transporteur à la BDD
        //Paramètre : aucun
        //Retourne (Transporteur)
        public function add() {
            $sql =  'INSERT INTO transporteur (libelle, email, franco_port, frais_port, id_tva, date_ajout, date_modif, valide) VALUES
                    ("'.Db::insertion($this->libelle).'", "'.Db::insertion($this->email).'", '.$this->franco_port.', '.$this->frais_port.', 
                    '.$this->tva->getId().', "'.$this->getDateAjout().'", "'.$this->getDateModif().'", '.intval($this->valide).')';
            $this->setId(Db::Execute($sql));
            return $this;
        }
        
        //Modifie un objet Transporteur dans la BDD
        //Paramètre : aucun
        //Retourne (Transporteur)
        public function update() {
            $sql =  'UPDATE transporteur
                    SET email = "'.Db::insertion($this->email).'",
                        franco_port = '.$this->franco_port.', 
                        frais_port = '.$this->frais_port.', 
                        id_tva = '.intval($this->tva->getId()).', 
                        date_modif = "'.$this->getDateModif().'",
                        valide = '.intval($this->valide).'
                    WHERE
                        id_transporteur = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
        
        //Désactive un Transporteur de la BDD
        //Paramètre : aucun
        //Retourne (Transporteur)
        public function suppr() {
            $this->setValide(false);
            return $this->update();
        }
        
        //Récupère tout les transporteurs 
        //Paramètre : OPTIONNEL (Booléen)valide
        //Retourne une collection de Transporteur
        public static function getTransporteurs($valide = true) {
            $sql =  'SELECT
                        t.id_transporteur as tid_transporteur, t.libelle as tlibelle, t.tel as ttel, t.email as temail, t.franco_port as tfranco_port, 
                        t.frais_port as tfrais_port, t.date_ajout as tdate_ajout, t.date_modif as tdate_modif, t.valide as tvalide, 
                        tv.id_tva as tvid_tva, tv.taux as tvtaux
                    FROM transporteur t
                    LEFT JOIN tva tv ON t.id_tva = tv.id_tva';
            if($valide) {
                $sql .= ' WHERE
                            t.valide = 1';
            }
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                $tabRetour[] = new Transporteur($p['tid_transporteur'], $p['tlibelle'], $p['ttel'], $p['temail'], $p['tfranco_port'], 
                                                $p['tfrais_port'], $tva, $p['tdate_ajout'], $p['tdate_modif'], $p['tvalide']);
            }
            
            return $tabRetour;
        }
        
        
        //Crée un objet Transporteur grâce à son ID
        //Paramètre : (int)id
        //Retourne (Transporteur)
        public static function rechercheTransporteur($id) {
            $sql =  'SELECT
                        t.id_transporteur as tid_transporteur, t.libelle as tlibelle, t.tel as ttel, t.email as temail, t.franco_port as tfranco_port, 
                        t.frais_port as tfrais_port, t.date_ajout as tdate_ajout, t.date_modif as tdate_modif, t.valide as tvalide, 
                        tv.id_tva as tvid_tva, tv.taux as tvtaux
                    FROM transporteur t
                    LEFT JOIN tva tv ON t.id_tva = tv.id_tva
                    WHERE
                        t.id_transporteur = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                return new Transporteur($p['tid_transporteur'], $p['tlibelle'], $p['ttel'], $p['temail'], $p['tfranco_port'], 
                                        $p['tfrais_port'], $tva, $p['tdate_ajout'], $p['tdate_modif'], $p['tvalide']);
            }
        }
		
        //GETTERS ET SETTERS
        public function getId() {return $this->id_transporteur;}
        public function getLibelle() {return $this->libelle;}
        public function getTel() {return Tools::getterPhone($this->tel);}
        public function getEmail() {return $this->email;}
        public function getFrancoPort() {return $this->franco_port;}
        public function getFraisPort() {return $this->frais_port;}
        public function getTva() {return $this->tva;}
        public function getDateAjout($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_ajout, $format);}
        public function getDateModif($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_modif, $format);}
        public function getValide() {return $this->valide;}

        public function setId($temp) {$this->id_transporteur = intval($temp);}
        public function setLibelle($temp) {$this->libelle = $temp;}
        public function setTel($temp) {$this->tel = Tools::setterPhone($temp);}
        public function setEmail($temp) {$this->email = $temp;}
        public function setFrancoPort($temp) {$this->franco_port = $temp;}
        public function setFraisPort($temp) {$this->frais_port = $temp;}
        public function setTva($temp) {$this->tva = $temp;}
        public function setDateAjout($temp) {$this->date_ajout = Tools::setterDate($temp);}
        public function setDateModif($temp) {$this->date_modif = Tools::setterDate($temp);}
        public function setValide($temp) {$this->valide = intval($temp);}
    }

?>